<?php
namespace common\jobs;

use common\models\Tally;
use common\models\TallyAccount;
use common\models\TallyCategory;
use common\models\TallyMember;
use common\models\TallyProject;
use common\models\TallyStore;
use common\services\ToolService;
use Yii;
use yii\base\BaseObject;
use yii\helpers\Json;
use yii\queue\JobInterface;

/**
 * Class NewTallyJob
 *
 * @package common\jobs
 * @desc 执行标准化处理 根据账本ID填充默认数据
 */
class NewTallyJob extends BaseObject implements JobInterface
{
    /**
     * @var $uid integer 用户id
     */
    public $uid;

    /**
     * @var $tallyId integer 账本ID
     * @desc 参数由外部必传
     */
    public $tallyId;

    /**
     * @param \yii\queue\Queue $queue
     * @return mixed
     * @throws
     * @desc 用户新增后触发的异步任务
     */
    public function execute($queue)
    {
        // 先获取账本信息
        $info = Tally::find()->where([
            'id' => $this->tallyId
        ])->one();
        if(!$info) {
            Yii::warning('账本初始化数据失败,账本不存在;id:'.$this->tallyId);
            return false;
        }
        $this->uid = $info->uid;
        // 写入默认分类 20201226改需 要提前初始化到数据库中
         $this->initDefaultCategory();
        // 写入默认账户
        $this->initDefaultAccount();
        // 写入默认项目
        $this->initDefaultProject();
        // 写入默认成员
        $this->initDefaultMember();
        // 写入默认商户
        $this->initDefaultStore();
        return true;
    }

    /**
     * @throws
     * 插入默认分类
     */
    public function initDefaultCategory()
    {
        $defaultCate = [
            '1'=> [
                [
                    'title' => '营业收入' ,
                    'children' =>  ['销售收入','服务收入']
                ],
                [
                    'title' => '金融投资收入' ,
                    'children' => ['利息收入','投资收入','退税收入']
                ],
                [
                    'title' => '营业外收入' ,
                    'children' => ['退货退款','卖废品','意外来钱']
                ]
            ],
            '2'=> [
                [
                    'title' => '货品材料' ,
                    'children' => ['进货支出','材料费']
                ],
                [
                    'title' => '人工支出' ,
                    'children' => ['员工薪资','招聘费','培训费']
                ],
                [
                    'title' => '运营费用' ,
                    'children' => ['税费','团队活动','市场活动','邮寄费','运营杂费']
                ],
                [
                    'title' => '办公费用' ,
                    'children' => ['房租水电物管','办公用品','通信费','交通费','差旅费','招待费']
                ]
            ]
        ];
        foreach ($defaultCate as $type => $cates) {
            foreach ($cates as $cate) {
                // 插入一级分类
                $model = new TallyCategory();
                $formData = [
                    'title' => $cate['title'],
                    'pic'   => '' ,
                    'tally_id'   => $this->tallyId,
                    'type'  => intval($type),
                    'pid'   => 0 ,
                    'uid'   => $this->uid,
                    'is_sys' => 0,
                ];
                $model->load($formData,'');
                $ret = $model->save();
                if($ret) {
                    // 插入子分类
                    $batchSon = [];
                    foreach ($cate['children'] as $sunCate) {
                        $formData['title'] = $sunCate;
                        $formData['pid'] = $model->id;
                        $batchData = $formData;
                        unset($batchData['children']);
                        $batchSon[] = array_values($batchData);
                    }
                    if(!empty($batchSon)) {
                        Yii::$app->db->createCommand()->batchInsert(
                            TallyCategory::tableName(),
                            array_keys($formData),
                            $batchSon
                            )->execute();
                    }
                }
            }
        }
    }

    /**
     * @throws
     * @desc 初始化默认账户
     */
    public function initDefaultAccount(){
        /*$defaultData = [
            ['title' => '现金' ,  'tag' => '现金账户'],
            ['title' => '基本户' ,  'tag' => '金融账户'],
            ['title' => '银行卡' ,  'tag' => '金融账户'],
            ['title' => '微信' , 'tag' => '虚拟账户'],
            ['title' => '支付宝' ,  'tag' => '虚拟账户'],
            ['title' => '信用卡' ,  'tag' => '信用账户'],
            ['title' => '生意借款' ,  'tag' => '负债账户'],
            ['title' => '应付款项' ,  'tag' => '负债账户'],
            ['title' => '应收款项' ,  'tag' => '债权账户'],
            ['title' => '基金账户' ,  'tag' => '投资账户'],
            ['title' => '股票账户' ,  'tag' => '投资账户'],
            ['title' => '其他账户' ,  'tag' => '其他账户'],
        ];*/
        $defaultData = ToolService::getConfig('DEFAULT_ACCOUNT_LIST');
        $defaultData = Json::decode($defaultData,true);
        if(!$defaultData) return ;
        $batchSon = [];
        $formData = [];
        foreach ($defaultData as $k => $formData) {
            $formData['tally_id'] = $this->tallyId;
            $formData['uid'] = $this->uid;
            $batchData = $formData;
            $batchSon[] = array_values($batchData);
        }
        if(!empty($batchSon)) {
            Yii::$app->db->createCommand()->batchInsert(
                TallyAccount::tableName(),
                array_keys($formData),
                $batchSon
            )->execute();
        }
    }

    /**
     * @throws
     * @desc 初始化默认成员
     */
    public function initDefaultMember(){
        $defaultData = [
            ['name' => '默认员工'],
        ];
        $batchSon = [];
        $formData = [];
        foreach ($defaultData as $k => $formData) {
            $formData['tally_id'] = $this->tallyId;
            $formData['uid'] = $this->uid;
            $batchData = $formData;
            $batchSon[] = array_values($batchData);
        }
        if(!empty($batchSon)) {
            Yii::$app->db->createCommand()->batchInsert(
                TallyMember::tableName(),
                array_keys($formData),
                $batchSon
            )->execute();
        }
    }

    /**
     * @throws
     * @desc 初始化默认项目
     */
    public function initDefaultProject(){
        $defaultData = [
            ['title' => '默认项目'],
        ];
        $batchSon = [];
        $formData = [];
        foreach ($defaultData as $k => $formData) {
            $formData['tally_id'] = $this->tallyId;
            $formData['uid'] = $this->uid;
            $batchData = $formData;
            $batchSon[] = array_values($batchData);
        }
        if(!empty($batchSon)) {
            Yii::$app->db->createCommand()->batchInsert(
                TallyProject::tableName(),
                array_keys($formData),
                $batchSon
            )->execute();
        }
    }

    /**
     * @throws
     * @desc 初始化默认客户/供应商
     */
    public function initDefaultStore(){
        $defaultData = [
            [
                'title' => '默认客户' ,
                'type' => 1
            ],
            [
                'title' => '默认供应商' ,
                'type' => 2
            ],
        ];
        $batchSon = [];
        $formData = [];
        foreach ($defaultData as $k => $formData) {
            $formData['tally_id'] = $this->tallyId;
            $formData['uid'] = $this->uid;
            $batchData = $formData;
            $batchSon[] = array_values($batchData);
        }
        if(!empty($batchSon)) {
            Yii::$app->db->createCommand()->batchInsert(
                TallyStore::tableName(),
                array_keys($formData),
                $batchSon
            )->execute();
        }
    }

}

